<template>
  <div>
    <el-button type="primary" @click="dialogFormVisible = true"
      >新增房间</el-button
    >
    <el-button type="primary" @click="$router.go(-1)">返回</el-button>
    <el-dialog title="新增酒店房间" :visible.sync="dialogFormVisible">
      <el-form
        ref="form"
        :rules="rules"
        :model="form"
        label-width="100px"
        style="width: 500px"
      >
        <el-form-item label="房间名称" prop="rname">
          <el-input v-model="form.rname" />
        </el-form-item>
        <el-form-item prop="area" label="房间大小">
          <el-input v-model="form.area">
            <template slot="append">平方米</template>
          </el-input>
        </el-form-item>
        <el-form-item prop="bedtype" label="床类型">
          <el-select v-model="form.bedtype" placeholder="请选择">
            <el-option value="大床">大床</el-option>
            <el-option value="双床">双床</el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="breakfast" label="是否含早餐">
          <template>
            <el-radio v-model="form.breakfast" label="含早餐">含早餐</el-radio>
            <el-radio v-model="form.breakfast" label="不含早餐"
              >不含早餐</el-radio
            >
          </template>
        </el-form-item>
        <el-form-item prop="window" label="是否有窗">
          <template>
            <el-radio v-model="form.window" label="有窗">有窗</el-radio>
            <el-radio v-model="form.window" label="无窗">无窗</el-radio>
          </template>
        </el-form-item>

        <el-form-item prop="rcount" label="房间数量">
          <el-input v-model="form.rcount">
            <template slot="append">间</template>
          </el-input>
        </el-form-item>
        <el-form-item prop="rprice" label="房间价格">
          <el-input v-model="form.rprice">
            <template slot="append">元/天</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
    <el-divider></el-divider>
    <el-table :data="hotelrooms">
      <el-table-column
        label="房间名称"
        prop="rname"
        width="150px"
      ></el-table-column>
      <el-table-column label="房间大小" prop="area" width="150px">
        <template slot-scope="scope">{{ scope.row.area }}平方米</template>
      </el-table-column>
      <el-table-column
        label="是否包含早餐"
        prop="breakfast"
        width="150px"
      ></el-table-column>
      <el-table-column
        label="床类型"
        prop="bedtype"
        width="150px"
      ></el-table-column>
      <el-table-column
        label="有无窗"
        prop="window"
        width="150px"
      ></el-table-column>
      <el-table-column label="价格" prop="rprice" width="150px">
        <template slot-scope="scope">{{ scope.row.rprice }}元/天</template>
      </el-table-column>
      <el-table-column label="数量" prop="rcount" width="150px">
        <template slot-scope="scope">{{ scope.row.rcount }}间</template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            title="修改酒店房间"
            size="small"
            type="warning"
            icon="el-icon-edit"
            @click="$router.push(`/home/hotelroom-update/${scope.row.rid}`)"
            circle
          ></el-button>
          <el-button
            title="删除酒店房间"
            size="small"
            type="danger"
            icon="el-icon-delete"
            circle
            @click="delItem(scope.row.rid)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import myaxios from '@/http/MyAxios'
export default {
  data() {
    return {
      dialogFormVisible: false,

      hotelrooms: [],
      form: {
        rname: '',
        area: '',
        bedtype: '',
        breakfast: '',
        window: '',
        rcount: '',
        rprice: '',
        hid: this.$route.params.hid,
      },
      rules: {
        // 整理表单验证规则
        rname: [{ required: true, message: '必填', trigger: 'blur' }],
        area: [{ required: true, message: '必填', trigger: 'blur' }],
        bedtype: [{ required: true, message: '必填', trigger: 'blur' }],
        breakfast: [{ required: true, message: '必填', trigger: 'blur' }],
        window: [{ required: true, message: '必填', trigger: 'blur' }],
        rcount: [{ required: true, message: '必填', trigger: 'blur' }],
        rprice: [{ required: true, message: '必填', trigger: 'blur' }],
      },
    }
  },
  methods: {
    delItem(rid) {
      console.log('要删除的序号:', rid)
      this.$confirm('该操作将永久删除酒店房间，是否继续？', '提示', {
        type: 'warning',
      }).then(() => {
        myaxios.delete(`http://localhost:3000/room/del/${rid}`).then(res => {
          this.$message({ type: 'success', message: '删除成功' })
          var params = this.$route.params.hid
          console.log(params)
          myaxios
            .get(`http://127.0.0.1:3000/room/info/query?hid=${params}`)
            .then(res => {
              console.log(res)
              this.hotelrooms = res.data.data
            })
        })
      })
    },
    submit() {
      console.log(this.form)
      this.$refs['form'].validate(valid => {
        if (valid) {
          // 发送请求，添加新酒店房间到数据库
          myaxios
            .post('http://127.0.0.1:3000/room/add', this.form)
            .then(res => {
              console.log('新增酒店房间', res)
              if (res.data.code == 200) {
                this.$message({
                  type: 'success',
                  message: '新增酒店房间成功',
                })
                this.dialogFormVisible = false
                myaxios
                  .get(
                    `http://127.0.0.1:3000/room/info/query?hid=${this.form.hid}`
                  )
                  .then(res => {
                    console.log(res)
                    this.hotelrooms = res.data.data
                  })
              }
            })
        }
      })
    },
  },
  mounted() {
    var params = this.$route.params.hid
    console.log(params)
    myaxios
      .get(`http://127.0.0.1:3000/room/info/query?hid=${params}`)
      .then(res => {
        console.log(res)
        this.hotelrooms = res.data.data
      })
  },
}
</script>

<style lang="scss" scoped>
.el-divider {
  margin: 24px 0 24px 0;
}
.el-select .el-input {
  width: 150px;
}
.input-with-select .el-input-group__prepend {
  background-color: #fff;
}
</style>
